<?php

namespace addons\shopro\library\XinYe;

use addons\shopro\library\XinYe\HttpClient;
use addons\shopro\library\XinYe\util\Xputil as Xutil;

class PrintService
{
    /**
     * 开发者ID(芯烨云后台登录账号）
     */
    var $user;
    /**
     * 芯烨云后台开发者密钥
     */
    var $userKey;
    /**
     * 当前UNIX时间戳，10位，精确到秒
     */
    var $timestamp;
    /**
     * 对参数 user + UserKEY + timestamp 拼接后（+号表示连接符）进行SHA1加密得到签名，值为40位小写字符串，其中 UserKEY 为用户开发者密钥
     */
    var $sign;
    /**
     * debug=1返回非json格式的数据。仅测试时候使用
     */
    var $debug;

    private const BASE_URL = 'https://open.xpyun.net/api/openapi';

    public function __construct()
    {
        $this->user      = 'zacow@163.com';
        $this->userKey   = '6c9e8d6f4917482db2a649735a79a20d';
        $this->debug     = 0;
        $this->timestamp = Xutil::getMillisecond();
        $this->sign      = Xutil::sign($this->user . $this->userKey . $this->timestamp);
    }

    private function xpyunPostJson($url, $request)
    {
        // print_r($this->user);die;
        $request['user']      = $this->user;
        $request['timestamp'] = $this->timestamp;
        $request['sign']      = $this->sign;
        $request['debug']     = $this->debug;
        $jsonRequest          = json_encode($request);
        $client               = new HttpClient();

        list($returnCode, $returnContent) = $client->http_post_json($url, $jsonRequest);

        return json_decode($returnContent, true);
    }

    /**
     * 1.批量添加打印机
     * @param restRequest
     * @return
     */
    public function xpYunAddPrinters($restRequest)
    {

        $url = self::BASE_URL . "/xprinter/addPrinters";
        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 2.设置打印机语音类型
     * @param restRequest
     * @return
     */
    public function xpYunSetVoiceType($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/setVoiceType";
        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 3.打印小票订单
     * @param restRequest - 打印订单信息
     * @return
     */
    public function xpYunPrint($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/print";

        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 4.打印标签订单
     * @param restRequest - 打印订单信息
     * @return
     */
    public function xpYunPrintLabel($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/printLabel";

        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 5.批量删除打印机
     * @param restRequest
     * @return
     */
    public function xpYunDelPrinters($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/delPrinters";

        return $this->xpyunPostJson($url, $restRequest);
    }


    /**
     * 6.修改打印机信息
     * @param restRequest
     * @return
     */
    public function xpYunUpdatePrinter($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/updPrinter";
        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 7.清空待打印队列
     * @param restRequest
     * @return
     */
    public function xpYunDelPrinterQueue($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/delPrinterQueue";
        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 8.查询订单是否打印成功
     * @param restRequest
     * @return
     */
    public function xpYunQueryOrderState($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/queryOrderState";

        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 9.查询打印机某天的订单统计数
     * @param restRequest
     * @return
     */
    public function xpYunQueryOrderStatis($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/queryOrderStatis";
        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 10.查询打印机状态
     *
     * 0、离线 1、在线正常 2、在线不正常
     * 备注：异常一般是无纸，离线的判断是打印机与服务器失去联系超过30秒
     * @param restRequest
     * @return
     */
    public function xpYunQueryPrinterStatus($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/queryPrinterStatus";

        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 10.批量查询打印机状态
     *
     * 0、离线 1、在线正常 2、在线不正常
     * 备注：异常一般是无纸，离线的判断是打印机与服务器失去联系超过30秒
     * @param restRequest
     * @return
     */
    public function xpYunQueryPrintersStatus($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/queryPrintersStatus";

        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 11.云喇叭播放语音
     * @param restRequest - 播放语音信息
     * @return
     */
    public function xpYunPlayVoice($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/playVoice";

        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 12.POS指令
     * @param restRequest
     * @return
     */
    public function xpYunPos($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/pos";

        return $this->xpyunPostJson($url, $restRequest);
    }

    /**
     * 13.钱箱控制
     * @param restRequest
     * @return
     */
    public function xpYunControlBox($restRequest)
    {
        $url = self::BASE_URL . "/xprinter/controlBox";

        return $this->xpyunPostJson($url, $restRequest);
    }
}

?>